<template>
	<view>
		<text>新用户限时福利</text>
		
		<view v-for="(item, index) in listData" :key="index">
			<view class="item_content">
				<text>{{item.applicableUnit}}</text>
				<text>{{item.preferentialInfo}}</text>
				<text>{{item.useTypeName}}</text>
				
			</view>
			
		</view>
		
		<text @click="getCoupon">一键领取</text>
		
	</view>
</template>

<script>
	import {getCouponDialog, receiveCoupons} from '@/common/apis/home-api.js';
	
	export default {
		computed: {
			tenantId() {
			    return this.$store.state.user.tenantId;
			},
		    deptId() {
		        return this.$store.state.user.deptId;
		    },
			isNotLogin() {
				return uni.$u.test.isEmpty(this.$store.state.user.userInfo);
			}
		},
		data() {
			return {
				listData:[]
			}
		},
		onLoad() {
			this.getData();
			
			uni.$on('login', res=>{
				this.getData();
			})
		},
		methods: {
			getData(){
				getCouponDialog({
					tenantId:this.tenantId,
					applicableUnitNo:this.deptId,
				}).then(res=>{
					this.listData = res.couponInfoVoList;
					if(uni.$u.test.isEmpty(res.couponInfoVoList)){
						uni.showToast({
							title:'暂无可领取优惠券',
							icon:'none'
						})
						uni.navigateBack()
					}
					
				}).catch(e=>{
					uni.showToast({
						title:'暂无可领取优惠券',
						icon:'none'
					})
					uni.navigateBack()
				})
			},
			
			getCoupon(){
				if (this.isNotLogin) {
					this.$store.dispatch('user/MmvLogin'); // 获取openid
					return
				}
				
				let list = this.listData.map(item=>
					item.couponId
				);
				
				receiveCoupons({
					couponIds:list
				}).then(res=>{
					uni.showToast({
						title:'恭喜您，领取成功',
						icon:'none'
					})
				})
			}
			
		}
	}
</script>

<style lang="scss" scoped>
.item_content{
	margin: 20upx;
	background-color: #fff;
	border-radius: 10upx;
}
</style>
